package com.yitiao.multithread.case06;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduleThreadExecutorDemo {
    public static void main(String[] args) {
        extracted();
    }

    private static void extracted() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("start---" + dateFormat.format(new Date()));
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        // initialDelay:第一次延迟 delay：第一次以后延迟多久执行一次
        executorService.scheduleWithFixedDelay(
                () -> {
//                    System.out.println(dateFormat.format(new Date()));
//                    // 遇到异常之后，不会抛出，且不会再继续执行任务。
////                    int a = 1/0;
////                    System.out.println("异常");
                    try {
                        System.out.println(dateFormat.format(new Date()));
                        int a = 1/0;
                        System.out.println("异常");
                    } catch (Exception e) {
                        System.out.println("发生异常：" + e.getMessage());
//                        throw e;
                    }
                },
                3000,
                1000,
                TimeUnit.MILLISECONDS
        );
    }
}
